<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>介绍管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <script src="/layui/layui.js" charset="utf-8"></script>
</head>
<body>
<!-- 表格操作按钮集 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑介绍</a>
</script>

<div id="updatediv" style="display:none; margin:10px;">
    <form class="layui-form" action="" lay-filter="formTest" id="form">
        <div class="layui-form-item">
            <label class="layui-form-label">介绍</label>
            <div class="layui-input-block">
                <textarea placeholder="请输入介绍" required  lay-verify="required" id="demo" autocomplete="off" name="title"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<table class="layui-hide" id="test" lay-filter="test"></table>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>

    layui.use(['table','form','layedit'], function(){
        var table = layui.table;//layui中使用html的table
        var $ = layui.$;//layui中使用jQuery
        var form = layui.form;
        var layedit = layui.layedit;
        var lay = layedit.build('demo');
        var usertab = table.render({//表格渲染
            elem: '#test'
            ,url:'/introduce/showAll'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,cols: [[//列  标题栏
                {field:'iid', title: '介绍id', width:90}
                ,{field:'bigtitle', title: '标题', width:160}
                ,{field:'title', title: '介绍'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:90}
            ]]
            ,page:true
        });

        table.on('tool(test)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                $("#form").append("<input type=\"hidden\" id=\"iid\" name=\"iid\" required  lay-verify=\"required\" autocomplete=\"off\" class=\"layui-input\">");
                var uplayer = layer.open({
                    type: 1
                    ,anim: 3
                    ,area: ['750px', '450px']
                    ,content: $('#updatediv')
                });
                //form自动赋值
                form.val("formTest",{
                    "iid":data.iid
                    ,"title":data.title
                });
                layedit.setContent(lay,data.title);
                //提交
                form.on('submit(formDemo)',function (data){
                    layer.close(uplayer);
                    $.ajax({
                        url:"/introduce/update"
                        ,type:"post"
                        ,data:{
                            "iid":$("#iid").val()
                            ,"title":layedit.getContent(lay)
                        },success:function(d){
                            if(d === 1){
                                layer.msg('修改成功',{
                                    icon:1,
                                    time:1000//不配置默认3秒
                                },function(){
                                    //更新表格
                                    usertab.reload({
                                        page: {
                                            curr: 1 //重新从第 1 页开始
                                        }
                                        ,where: {
                                            iid:null
                                        }
                                    });
                                });
                            }
                        }
                    });
                    return false;//阻止表单跳转
                });
            }
        });
    });
</script>
</body>
</html>